﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Tasks
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void ExecuteButton_Click(object sender, EventArgs e)
        {
            textSource.Text = textSource.Text.EnsureEndsWith("\\");
            textDest.Text = textDest.Text.EnsureEndsWith("\\");

            // Validate
            var msg = "";
            if (string.IsNullOrEmpty(textSource.Text))
                msg += "Source must not be empty!\r";

            else if (!Directory.Exists(textSource.Text))
                msg += "Source directory does not exist!";
            
            if (string.IsNullOrEmpty(textDest.Text))
                msg += "Destination must not be empty!\r";
            
            if (checkChange.Checked && string.IsNullOrEmpty(textNS.Text))
                msg += "Namespace must not be empty!\r";

            if (string.IsNullOrEmpty(textJQueryTheme.Text))
                msg += "JQuery UI Theme must not be empty!\r";
            else if (!Directory.Exists(Path.Combine(textSource.Text + "Content\\css\\jquery\\" + textJQueryTheme.Text)))
                msg += "JQuery UI theme does not exist!";

            if (msg.Length > 0)
            {
                MessageBox.Show(msg);
                return;
            }

            // Delete the dest directory
            if (Directory.Exists(textDest.Text))
            {
                var dir = new DirectoryInfo(textDest.Text);
                dir.Delete(true);
            }

            // Execute the task
            CopyTask.Execute(textDest.Text, textSource.Text, textJQueryTheme.Text);

            // Change the namespace if required
            if (checkChange.Checked)
                ChangeNamespaceTask.Execute(textDest.Text, textNS.Text);

            MessageBox.Show("Operation Complete!");

            textDest.Text.EnsureEndsWith("\\");
            Process.Start(textDest.Text);
        }

        private void cmdExecuteJSMerge_Click(object sender, EventArgs e)
        {
            textJSSource.Text = textJSSource.Text.EnsureEndsWith("\\");
            
            // Validate
            if (string.IsNullOrEmpty(textJSSource.Text))
            {
                MessageBox.Show("Javascript files source folder is incorrect!");
                return;
            }
            else if (!Directory.Exists(textJSSource.Text))
            {
                MessageBox.Show("Source directory does not exist!");
                return;
            }

            // Execute
            MergeJSTask.Execute(textJSSource.Text);
            MessageBox.Show("Operation Completed!");

            // Open the folder
            Process.Start(textJSSource.Text);

        }

        private void buttonExecuteBigfootCSSMerge_Click(object sender, EventArgs e)
        {
            textBigfootCSSSource.Text = textBigfootCSSSource.Text.EnsureEndsWith("\\");
            textBigfootCSSDest.Text = textBigfootCSSDest.Text.EnsureEndsWith("\\");

            // Validate
            var msg = "";
            if (string.IsNullOrEmpty(textBigfootCSSSource.Text) || string.IsNullOrEmpty(textBigfootCSSDest.Text))
                msg += "Source and destination folders are required!";
            else if (!Directory.Exists(textBigfootCSSSource.Text) || !Directory.Exists(textBigfootCSSDest.Text))
                msg += "Either source or destination directory does not exist";
            if (msg.Length > 0)
            {
                MessageBox.Show(msg);
                return;
            }

            // Execute
            MergeBigfootCSSTask.Execute(textBigfootCSSSource.Text, textBigfootCSSDest.Text);
            MessageBox.Show("Task Completed!");

            // Open the folder
            Process.Start(textBigfootCSSDest.Text);
        }


    }
}
